<template>
  <div>
    <Search @SearchBtn="SearchBtnFn">
      <template>
        <el-form-item>
          <template>
            <div class="text">{{ this.$route.meta.BtnText && this.$route.meta.BtnText.user }}</div>
            <el-select v-model="pageS.state" placeholder="请选择" style="width: 228px;">
              <el-option v-for="(item, index) in data" :key="index" :label="item.text" :value="item.core" />
            </el-select>
          </template>
        </el-form-item>
      </template>
      <template>
        <el-form-item>
          <template>
            <div class="text">{{ this.$route.meta.BtnText && this.$route.meta.BtnText.total }}</div>
            <el-cascader v-model="doctorValue" :props="props" :options="options" @focus="props.lazy = true" />
          </template>
        </el-form-item>
      </template>
    </Search>
    <Table
      :table-data="tableData"
      :btn-text="'新增货品'"
      :btn-text-two="'生成盘点任务'"
      :pagecount="pagecount"
      @TypeChange="TypeChangeFn"
      @TopBtn="TopBtnFn"
      @CurrentChange="CurrentChangeFn"
      @SizeChange="SizeChangeFn"
      @OneBtn="OneBtnFn"
    />
  </div>
</template>

<script>
import { AllCheck, AllShipper, GetAllWarehouseList, AllReservoir, GetAllWarehouseList } from '@/api'
export default {
  data() {
    return {
      // 渲染数据
      tableData: [],
      // 后端页码
      pageS: {
        size: 10,
        current: 1
      },
      // 页码页数
      pagecount: {},
      // 联级选择器
      options: [],
      doctorValue: [],
      props: {
        lazy: false,
        lazyLoad: this.AllWarehouseChildren
      },
      data: []
    }
  },
  created() {
    this.AllCheckFn()
    this.AllShipperFn()
    this.AllWarehouse()
  },
  methods: {
    // 渲染数据
    async AllCheckFn() {
      const res = await AllCheck(this.pageS)
      this.pagecount = res.data.data
      res.data.data.records.forEach((item) => {
        const obj = {
          code: item.code,
          name: item.ownerName,
          type: this.$store.state.PublicData.PDstatus[item.status],
          location: item.createTime,
          address: item.planCheckTime,
          status: this.$store.state.PublicData.PDreason[item.reason],
          surface: this.$store.state.PublicData.PDdimension[item.dimension],
          includedNum: this.$store.state.PublicData.PDtype[item.type],
          personName: item.warehouseName,
          phone: item.areaName,
          updateTime: item.createName,
          id: item.id
        }
        this.tableData.push(obj)
      })
    },
    // 货主数据
    async AllShipperFn() {
      const res = await AllShipper()
      console.log(res)
    },
    // 主货区
    async AllWarehouse() {
      const res = await GetAllWarehouseList({ status: 1 })
      res.data.data.forEach((item) => {
        this.options.push({
          value: item.id,
          label: item.name,
          children: []
        })
      })
      console.log(this.options)
    },
    // 子仓库
    async AllWarehouseChildren(node, resolve) {
      console.log(node)
      this.options.forEach((item) => {
        item.children = []
      })
      const res = await AllReservoir({ status: 1, warehouseId: node.value })
      res.data.data.forEach((item) => {
        this.options.forEach((chir) => {
          chir.children.push({
            label: item.name,
            value: item.id
          })
        })
      })
      console.log(this.options)
      node.level >= 0
      resolve(res.data.data)
    },
    // 搜索
    SearchBtnFn(Form) {
      console.log(Form)
      this.pageS.code = Form.innerCooder
      this.pageS.name = Form.name
      if (Form.state !== 2 && Form.state !== '') {
        this.pageS.status = Form.state
      }
      this.AllCheckFn()
      this.tableData = []
    },
    checkFn() {
      console.log(1)
    },
    TypeChangeFn() { },
    // 新建按钮
    TopBtnFn() {
      this.$router.push({
        path: '/manage-base-info/area/details'
      })
    },
    // 编辑按钮
    OneBtnFn(row) {
      console.log(this.$route)
      this.$router.push({
        path: '/manage-base-info/area/details',
        query: {
          Id: row.id
        }
      })
    },
    // 页数跳转
    CurrentChangeFn(val) {
      this.pageS.current = val
      this.AllCheckFn()
      this.tableData = []
    },
    SizeChangeFn(val) {
      this.pageS.size = val
      this.AllCheckFn()
      this.tableData = []
    }
  }
}
</script>

<style>

</style>
